package com.sc.nft.task;

import com.sc.nft.entity.TradingPoolConfig;
import com.sc.nft.service.TradingPoolConfigService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 *交易池定时任务
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class TradingPoolTask {

    private final TradingPoolConfigService tradingPoolConfigService;

    @XxlJob("destroyTradingPool")
    public void destroyTradingPool() {
        log.info("销毁交易池");
        List<TradingPoolConfig> activatedTradingPoolConfigList = tradingPoolConfigService.getActivatedTradingPoolConfigList();
        for (TradingPoolConfig tradingPoolConfig : activatedTradingPoolConfigList) {
            try {
                tradingPoolConfigService.destroyTradingPool(tradingPoolConfig);
            } catch (Exception e) {
                // 不抛出异常 否则的话没办法继续往下走了.
                log.error("销毁交易池失败,失败id:{},失败内容:", tradingPoolConfig.getId(), e);
            }
        }
    }


    /**
     * 交易数据初始化
     *
     */
    @XxlJob("initTradingPool")
    public void tradingPoolTransactionDataInit() {
        tradingPoolConfigService.initTradingPool();
    }
}
